package com.woniuxy.userservice.rabbit.queue;

import com.woniuxy.util.MyRedisKeyUtil;
import com.woniuxy.util.MyRedisUtil;
import com.woniuxy.util.MySmsUtil;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserLoginQueue {
    @Autowired
    private MySmsUtil mySmsUtil;
    @Autowired
    private MyRedisUtil redisUtil;
    @Autowired
    private MyRedisKeyUtil redisKeyUtil;

    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(value = "code-send-login")})
    public void codeSendLogin(String phone) throws Exception {
        String phoneCode = mySmsUtil.send(phone);//通过互亿验证码发送工具生成验证码，发送短信并返回
        //缓存里的key要具有可识别度，唯一性，规范
        redisUtil.set(redisKeyUtil.loginCodeKey(phone),phoneCode,5*60);//验证码有效期5分钟
        redisUtil.set(redisKeyUtil.sendLoginInMinute(phone),"0",60);//限制登录一分钟内不能重复发
        if (!redisUtil.exists(redisKeyUtil.sendCodeTotalInHour(phone))){
            redisUtil.set(redisKeyUtil.sendCodeTotalInHour(phone),"1",60*60);
        }else {
            redisUtil.increment(redisKeyUtil.sendCodeTotalInHour(phone));//1小时内验证发送次数
        }
    }
}
